package com.xx.news;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.xx.news.activity.JingxuanActivity;
import com.xx.news.activity.QicheActivity;
import com.xx.news.activity.ShipinActivity;
import com.xx.news.activity.ToutiaoActivity;
import com.xx.news.activity.XinwenActivity;
import com.xx.news.activity.YuleActivity;
import com.xx.news.activity.YunDongActivity;
import com.xx.news.base.BaseActivity;
import com.xx.news.databinding.ActivityNewsBinding;
import com.xx.news.viewmodel.NewViewModel;

import java.util.ArrayList;
import java.util.List;

public class NewsActivity extends BaseActivity<ActivityNewsBinding, NewViewModel> {

    private String[] titles = new String[]{"头条",
            "精选",
            "娱乐",
            "汽车",
            "新闻",
            "运动",
            "视频"
    };
    private ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    public void initData() {
        ToutiaoActivity fragment1 = new ToutiaoActivity();
        JingxuanActivity fragment2 = new JingxuanActivity();
        YuleActivity fragment3 = new YuleActivity();
        QicheActivity fragment4 = new QicheActivity();
        XinwenActivity fragment5 = new XinwenActivity();
        YunDongActivity fragment6 = new YunDongActivity();
        ShipinActivity fragment7 = new ShipinActivity();
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        fragments.add(fragment4);
        fragments.add(fragment5);
        fragments.add(fragment6);
        fragments.add(fragment7);

        MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments, titles);
        binding.viewPager1.setAdapter(pagerAdapter);
        binding.tabs.setupWithViewPager(binding.viewPager1);
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_news;
    }

    static class MyPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragments;
        private String[] titles;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> mFragments, String[] titles) {
            super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            this.mFragments = mFragments;
            this.titles = titles;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
